<?php
namespace app\home\controller;

//引入我们的公共的控制器
use app\common\controller\Home;

class Index extends Home
{
    public $NoLogin = ['*'];

    public function __construct(){
        //继承父类的构造函数
        parent::__construct();

        //加载模型
        $this->BusinessModel = model('Business.Business');
        $this->SubjectModel = model('Subject.Subject');
        $this->TeacherModel = model('Teacher.Teacher');
        $this->CategoryModel = model('Subject.Category');
    }

    //首页
    public function index()
    {
        $top = $this->SubjectModel->group('cateid')->order('id','desc')->limit(6)->select();

        $subject = $this->SubjectModel->order('id','desc')->limit(10)->select();

        $teacher = $this->TeacherModel->limit(10)->select();

        $this->assign([
            'top' => $top,
            'subject' => $subject,
            'teacher' => $teacher,
        ]);

        return $this->view->fetch();
    }

    //注册
    public function register(){
        // 判断是否有推荐人
        $token = $this->request->param('token','','trim');

        if(!empty($token))
        {
            //模仿加密方式，把这个推广的用户给他找出来
            $list = $this->BusinessModel->field(['id', 'mobile'])->select();

            if($list)
            {
                foreach($list as $item)
                {
                    //加密
                    $value = md5($item['id'].$item['mobile']);

                    //如果是一样的，就找到了推广人
                    if($token == $value)
                    {
                        //就将推广人存入到cookie里面,然后终止循环
                        cookie('token', $token);
                        break;
                    }

                }
            }

        }

        //判断是否有过来post请求
        if($this->request->isPost()){
            $mobile = $this->request->param('mobile','','trim');
            $password = $this->request->param('password','','trim');

            if(empty($password)){
                $this->error('密码不能为空');
                exit;
            }

            //生成密码盐
            $salt = randstr(10);  //application/common.php

            //加密密码
            $repass = md5($password.$salt);

            //查寻注册的来源渠道， 加载模型 会自动去application/common/Business/Source
            $sourceid = model('Business.Source')->where(['name' => ['LIKE',"%云课堂%"]])->value('id');

            //模型sql语句打印
            // $sql = model('Business.Source')->where(['name' => ['LIKE',"%云课堂%"]])->getLastSql();
            // echo $sql;

            // var_dump($sourceid);
            // exit;


            //组装数据
            $data = [
                'mobile' => $mobile,
                'nickname' => build_encrypt($mobile,3,4),  //脱敏显示
                'salt' => $salt,
                'password' => $repass,
                'sourceid' => $sourceid,
                'gender' => '0',
                'deal' => '0',
                'money' => '0',
                'auth' => '0',
            ];

            // 找推荐人
            // 判断是否没有推荐人
            $token = $this->request->param('token', '', 'trim');
            $token = empty($token) ? cookie('token') : $token;

            //绑定推荐人
            if(!empty($token))
            {
                //模仿加密方式，把这个推广的用户给他找出来
                $list = $this->BusinessModel->field(['id', 'mobile'])->select();

                if($list)
                {
                    foreach($list as $item)
                    {
                        //加密
                        $value = md5($item['id'].$item['mobile']);

                        //如果是一样的，就找到了推广人
                        if($token == $value)
                        {
                            //就将推广人存入到cookie里面,然后终止循环
                            $data['parentid'] = $item['id'];
                            break;
                        }

                    }
                }

            }

            //插入数据
            //validate()验证器 成功返回插入的条数 失败返回false
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            if($result === FALSE){
                //插入失败 让模型来返回错误信息
                $this->error($this->BusinessModel->getError());
                exit;
            }else{
                $this->success('注册成功，请登录',url('/home/index/login'));
                exit;
            }
        }

        return $this->view->fetch();
    }

    //登录
    public function login(){
        //判断是否有post请求
        if($this->request->isPost()){
            // 接收参数
            $mobile = $this->request->param('mobile','','trim');
            $password = $this->request->param('password','','trim');

            // 查询用户是否存在
            $business = $this->BusinessModel->where(['mobile'=>$mobile])->find();

            if(empty($business)){
                $this->error('用户不存在');
                exit;
            }
            // 打印信息
            // var_dump($business->toArray());
            // exit;

            // 验证密码是否正确
            $salt = $business['salt'];
            $repass = md5($password.$salt);

            if($repass != $business['password']){
                $this->error('密码错误');
                exit;
            }

            // 存入手机号码和ID
            cookie('mobile',$business['mobile']);
            cookie('busid',$business['id']);

            //获取是否有登录后的跳转界面
            $redirect = cookie('redirect');

            if($redirect)
            {
                $this->success('登录成功', $redirect);
                exit;
            }else
            {
                $this->success('登录成功', url('home/business/index'));
                exit;
            }

        }
        //访问地址：www.fast.com/index.php/home/index/login
        //view/index/login.html
        return $this->view->fetch();
    }

    //退出
    public function logout(){
        //ajax返回值
        $success = [
            'result' => false,
            'msg' => ''
        ];
        // 判断是否是Ajax请求
        if($this->request->isAjax()){
            //删除掉cookie信息
            cookie('busid',NULL);
            cookie('mobile',NULL);

            //自动根据语境返回json的结果
            $this->success('退出成功');
        }else{
            $this->error('退出失败');
            exit;
        }
    }
}
